<?php
namespace app\appoint\controller;
use app\AdminRest;
use app\ApiRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\appoint\model\IndexRecord as record;
use app\appoint\model\IndexProject;
use app\shop\model\IndexCardCount;


class IndexRecord extends ApiRest
{

    protected $model;
    protected $pro_model;
    protected $count_model;


    public function __construct(App $app) {
        parent::__construct($app);
        $this->model       = new record();
        $this->pro_model   = new IndexProject();
        $this->count_model = new IndexCardCount();
        $this->model->changeStatus($this->_uniacid);
    }
    /**
     * 预约首页
     */
    public function recordAdd(){
        $input = $this->_input;
        if(lbIsStaff($input['staff_id'])!=1){
            return $this->error('not find card');
        }
        $range = explode( '-', $input['range']);

        $date_start = $input['date'] . $range[ 0 ] . ':00';
        $date_end   = $input['date'] . $range[ 1 ] . ':00';
        $date_start = strtotime( $date_start );
        $date_end   = strtotime( $date_end );

        $date_end = $date_end>$date_start?$date_end:$date_end+86400;
        if ( $date_start < time() ) {
            return $this->error('You cant choose the time period that has passed');
        }
        $pro = $this->pro_model->proInfo(['id'=>$input['id']]);
        if(empty($pro)){
            return $this->error('not data');
        }
        $check = $this->model->recordCount(['project_id'=>$input['id'],'status'=>1,'to_uid'=>$input['staff_id']]);

        if ($check >= $pro['limit']&&$pro['limit']!=0) {
            return $this->error('Limit errors');
        }
        $user_record = $this->model->recordInfo([
            'start_time' => $date_start,
            'end_time'   => $date_end,
            'project_id' => $input['id'],
            'user_id'    => $this->getUserId(),
            'status'     => 1
        ]);
        if(!empty($user_record)){
            return $this->error('You have made an appointment');
        }

        $insertData = array(
            'user_id'    => $this->getUserId(),
            'to_uid'     => $input['staff_id'],
            'project_id' => $input['id'],
            'name'       => $input['name'],
            'phone'      => $input['phone'],
            'start_time' => $date_start,
            'end_time'   => $date_end,
            'remark'     => $input['remark'],
            'status'     => 1,
            'uniacid'    => $this->_uniacid,
            'address'    => $input['address']
        );
        $recordId = $this->model->recordAdd($insertData);
        if ( $this->getUserId() != $input['staff_id'] ) {
            $insertData = array(
                'user_id' => $this->getUserId(),
                'to_uid'  => $input['staff_id'],
                'sign'    => 'order',
                'type'    => '3',
                'uniacid' => $this->_uniacid,
                'target'  => $recordId,
            );
            $insert_id = $this->count_model->countAdd($insertData);
            $push_data = array(
                'action'  => 'longbingSendWxServiceNotice',
                'event'   => 'longbingSendWxServiceNotice',
                'count_id'=> $insert_id
            );
            publisher(json_encode($push_data ,true));
        }
        return $this->success(true);
    }


    /**
     *
     * 我的预约列表
     */
    public function recordList(){
        $input = $this->_param;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        if(!empty($input['status'])||($input['status']===0&&!empty($input['staff_id']))){
            $dis[] = ['a.status','=',$input['status']];
        }else{
            $dis[] = ['a.status','>',-1];
        }
        if(!empty($input['staff_id'])){
            $dis[] = ['a.to_uid','=',$input['staff_id']];
        }else{
            $dis[] = ['a.user_id','=',$this->getUserId()];
        }
        $list = $this->model->recordList($dis);
        return $this->success($list);
    }

    /**
     *
     * 预约详情
     */
    public function recordInfo(){
        $input = $this->_param;
        $data  = $this->model->recordProInfo(['a.id'=>$input['id']]);
        if(empty($data)){
            return $this->error('not data');
        }
        return $this->success($data);
    }

    /**
     *
     *  取消|使用预约
     */
    public function recordChange(){
        $input = $this->_input;
        $data  = $this->model->recordInfo(['id'=>$input['id'],'user_id'=>$this->getUserId(),'status'=>1]);
        if(empty($data)){
            return $this->error('not data');
        }
        $res   = $this->model->recordUpdate(['id'=>$input['id']],['status'=>$input['status']]);

        return $this->success($res);
    }










}
